﻿Imports System.Data

Partial Class Interfaz_frmBusClientes
    Inherits System.Web.UI.Page
    Dim CL As New ControladorLogica
    Dim dtDatos As New DataTable
    Dim dataset As New DataSet
    Dim data As New DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If CL.visibilidadesFormsInternos("Busqueda", Session("user").ToString) = False Then
            Response.Redirect("Index.aspx")
        End If
        Dim Array As ArrayList
        Dim i As Integer

        Array = CL.obtenerCliente()


        If Not Page.IsPostBack Then
            Me.DropRegion.Items.Add("I")
            Me.DropRegion.Items.Add("II")
            Me.DropRegion.Items.Add("III")
            Me.DropRegion.Items.Add("IV")
            Me.DropRegion.Items.Add("V")
            Me.DropRegion.Items.Add("VI")
            Me.DropRegion.Items.Add("VII")
            Me.DropRegion.Items.Add("VIII")
            Me.DropRegion.Items.Add("IX")
            Me.DropRegion.Items.Add("X")
            Me.DropRegion.Items.Add("XI")
            Me.DropRegion.Items.Add("XII")

            For i = 0 To Array.Count - 1
                Me.DropGiro.Items.Add(DirectCast(Array.Item(i), Cliente).Giro)
                Me.DropCiudad.Items.Add(DirectCast(Array.Item(i), Cliente).Ciudad)
                Me.DropRut.Items.Add(DirectCast(Array.Item(i), Cliente).Rut)
            Next
        End If

        'se guardan datos en seccion para el positback de la pagina respecto al los datos del datagridview
        If Session("busquedaCliente") Is Nothing Then
            Session("busquedaCliente") = dtDatos
        Else
            dtDatos = DirectCast(Session("busquedaCliente"), DataTable)
        End If

        If Not Page.IsPostBack Then
            Try
                dtDatos.Columns.Add(New DataColumn("RUT"))
                dtDatos.Columns.Add(New DataColumn("RZNSOCIAL"))
                dtDatos.Columns.Add(New DataColumn("CIUDAD"))
                dtDatos.Columns.Add(New DataColumn("COMUNA"))
                dtDatos.Columns.Add(New DataColumn("REGION"))
                dtDatos.Columns.Add(New DataColumn("TELEFONO"))
                dtDatos.Columns.Add(New DataColumn("GIRO"))
                dtDatos.Columns.Add(New DataColumn("DIRECCION"))
                dtDatos.Columns.Add(New DataColumn("FAX"))
                dtDatos.Columns.Add(New DataColumn("EMAIL"))

                Me.GridCliente.DataBind()
                Me.GridCliente.DataSource = Me.dtDatos
            Catch
            End Try
        End If
        'Me.GridActividad.data.DataBind()
        Me.GridCliente.DataSource = Me.dtDatos
    End Sub

#Region "Funciones"
    Public Sub Mensaje(ByVal msg As String)
        Dim alertScript As String = "<script language=JavaScript runat=server>"
        alertScript &= "alert('" & msg & "');"
        alertScript &= "</script>"
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "clientScrip", alertScript)
    End Sub

    Private Sub buscar()

        Dim ArrayClient As New ArrayList
        Dim i As Integer
        Dim query As String

        'Busqueda por rut
        If R1.Checked = True Then
            query = "select * from cliente where rut ='" + DropRut.SelectedItem.Value + "'"
            ArrayClient = CL.obtenerClienteGeneral(query)

        Else
            'Busqueda por region
            If R2.Checked = True Then
                query = "select * from cliente where region ='" + DropRegion.SelectedItem.Value + "'"
                ArrayClient = CL.obtenerClienteGeneral(query)
            Else
                'Busqueda por giro
                If R3.Checked = True Then
                    query = "select * from cliente where giro ='" + DropGiro.SelectedItem.Value + "'"
                    ArrayClient = CL.obtenerClienteGeneral(query)
                Else
                    'Busqueda por ciudad
                    If R4.Checked = True Then
                        query = "select * from cliente where ciudad ='" + DropCiudad.SelectedItem.Value + "'"
                        ArrayClient = CL.obtenerClienteGeneral(query)
                    Else
                        If R5.Checked = True Then
                            query = "select * from cliente where rznsocial ='" + Me.TxTRznSocial.Text + "'"
                            ArrayClient = CL.obtenerClienteGeneral(query)
                        Else
                            query = "select * from cliente"
                            ArrayClient = CL.obtenerClienteGeneral(query)
                        End If
                    End If
                End If
            End If
        End If

        Try

            For i = 0 To ArrayClient.Count - 1

                Dim drDatos As DataRow = dtDatos.NewRow
                drDatos.Item("RUT") = DirectCast(ArrayClient.Item(i), Cliente).Rut
                drDatos.Item("RZNSOCIAL") = DirectCast(ArrayClient.Item(i), Cliente).RznSocial
                drDatos.Item("CIUDAD") = DirectCast(ArrayClient.Item(i), Cliente).Ciudad
                drDatos.Item("COMUNA") = DirectCast(ArrayClient.Item(i), Cliente).Comuna
                drDatos.Item("REGION") = DirectCast(ArrayClient.Item(i), Cliente).Region
                drDatos.Item("TELEFONO") = DirectCast(ArrayClient.Item(i), Cliente).Telefono
                drDatos.Item("GIRO") = DirectCast(ArrayClient.Item(i), Cliente).Giro
                drDatos.Item("DIRECCION") = DirectCast(ArrayClient.Item(i), Cliente).Direccion
                drDatos.Item("FAX") = DirectCast(ArrayClient.Item(i), Cliente).Fax
                drDatos.Item("EMAIL") = DirectCast(ArrayClient.Item(i), Cliente).Email

                dtDatos.Rows.Add(drDatos)
            Next
            Me.GridCliente.DataSource = dtDatos
            Me.GridCliente.DataBind()
            Me.GridCliente.ForeColor = Drawing.Color.Blue

        Catch ex As Exception
            Me.Mensaje(ex.ToString)
        End Try

        clean()
    End Sub

    Private Sub clean()
        Me.R1.Checked = False
        Me.R2.Checked = False
        Me.R3.Checked = False
        Me.R4.Checked = False
        Me.R5.Checked = False
    End Sub

    Private Sub vaciaGrid()
        data = DirectCast(Session("busquedaCliente"), DataTable)
        If Not Data Is Nothing Then
            Data.Clear()
            Session("busquedaCliente") = data
        End If
    End Sub
    Private Sub limpiar()
        clean()
        Me.TxTRznSocial.Text = String.Empty
    End Sub

#End Region

#Region "Botones"

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        vaciaGrid()
        buscar()
        limpiar()
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        limpiar()
    End Sub
#End Region
    
End Class
